<?php
/**
 * 附件上传信息管理
 * @copyright   2011 友拓软件友拓通
 * @category   	附件上传信息管理
 * @package  	Action
 * @version 	2011-03-23
 * @author    	黄明兴
 */
class AjaxUploadifyPublicAction extends Action{

	// 通过uploadidy上传附件
	public function uploads() {
		if(true === empty($_FILES)) {return ;}
		$file 				= $_FILES['file_upload'];
		$file['savename'] 	= getRands(15);
		$file_parts			= pathinfo($file['name']);
		$file_type 			= $file_parts['extension'];
		$file['type']		= $file_parts['extension'];
		$file_name 			= $file['savename'].'.'.$file_type;
		$cpation_name 		= $file['name'];
		$relation_type 		= $_POST['type'];
		$relation_id		= intval($_POST['id']);
		$tocken				= $_POST['tocken'];
//		$pic_name			= $file['name']; // 用户输入的图片名字
		$return_type		= $_POST['rdata']; // 上传成功后返回的数据格式
		import("ORG.Util.UploadFile");
		$upload 		= new UploadFile();
		$upload->thumb 	= true;
		if(!empty($_POST['allowTypes'])){
			$upload->allowTypes=$this->setAllowTypes($_POST['allowTypes']);
		}
		$file_dir 		= self::importDir($relation_type);
		if (!$upload->uploadOne($file,UPLOADS_PATH.$file_dir)) {
			$info['id']	= -1;
			$info['error']=$upload ->getErrorMsg();  
		    echo json_encode($info);                 
		} else {
			$data = array(
						'file_url'		=> $file_name,
						'relation_id'	=> $relation_id,
						'relation_type'	=> $relation_type,
						'cpation_name'	=> $cpation_name,
						'tocken'		=> $tocken,
//						'file_name'		=> $pic_name
					);
			$id = M('Gallery')->add($data);
			if ($return_type=='json') {
				echo json_encode(array('id'=>$id,'file'=>$file_dir.$file_name,'cpation_name'=>$cpation_name,'size'=>round($file['size']/1024,2),'relation_type'=>$relation_type,'file_url'=>$file['savename']));
			}else {
				echo $id;
			}
		    
		}
	}
	
	/**
	 * 根据图片类别返回图片存储目录
	 *
	 * @param string $relation_type
	 * @return string
	 */
	private function importDir($relation_type){
		$dis = C('UPLOAD_DIR');
		return $dis[$relation_type];
	}
	/**
	 * 设置上传文件允许的类型
	 *
	 * @param string $info
	 * @return array
	 */
	public function setAllowTypes($info){
		$allowTypes	= @explode(',',$info);
		return $allowTypes;	
	}
	
	// 删除附件
	public function deletes(){
		$id = intval($_REQUEST['id']);
		if (empty($id)) return false;
		$rs = M('Gallery')->find($id);
		@unlink(UPLOADS_PATH.self::importDir($rs['relation_type']).$rs['file_url']);
		@unlink(UPLOADS_PATH.self::importDir($rs['relation_type']).'small_'.$rs['file_url']);
		M('Gallery')->delete($id);
	}
	
	//通过名字删除图片
	public function deletesName(){ 
		$model	=	M('Gallery');
		$name = trim($_REQUEST['name']);
		if (empty($name)) return false;
		$rs = $model->where('tocken!="" and cpation_name=\''.$name.'\'')->find();
		@unlink(UPLOADS_PATH.self::importDir($rs['relation_type']).$rs['file_url']);
		@unlink(UPLOADS_PATH.self::importDir($rs['relation_type']).'small_'.$rs['file_url']);
		$model->delete($rs['id']); 
	}
	
	
	// 通过uploadidy上传附件直接保存不入库
	public function upload() {
		if(true === empty($_FILES)) {return ;}
		$file 				= $_FILES['Filedata'];
		$file['savename'] 	= $_POST['fname'];
		$file_parts			= pathinfo($file['name']);
		$file_type 			= $file_parts['extension'];
		$file_name 			= $file['savename'].'.'.$file_type;
		$tocken				= $_POST['tocken'];
		$relation_type 		= $_POST['type'];
		import("ORG.Util.UploadFile");
		$upload 		= new UploadFile();
		$upload->uploadReplace 	= true;
		$file_dir 		= self::importDir($relation_type);
		if (!$upload->uploadOne($file,UPLOADS_PATH.$file_dir)) {   
		    echo $upload ->getErrorMsg();                  
		} else {
			echo $file['savename'];
		}
	}
}?>